<?php 
class accountsAction extends AdminMaster{
	public function listFilter(){
		$this->data['filter']['groups']	= SysAccountGroups::getDataListFrontEnd();
	
	}
	public function showList() {
		$this->data ['list'] = SysAccounts::getDataList ( $this->data ['filter'], $this->data ['pagination'] );
		$this->data ['pagination'] ['total'] = SysAccounts::getDataListCount ( $this->data ['filter'] );
		$this->data ['pagination'] ['router'] = 'sys_account';
		$this->data ['pagination'] ['params'] = $this->data ['filter'];
		#print_r($this->data);die;
	}
	public function delRecord() {
		SysAccountsTable::delete ( $this->data ['select_list'] );
	}
	public function disableRecord() {
		SysAccountsTable::disable ( $this->data ['select_list'] );
	}
	public function enableRecord() {
		SysAccountsTable::enable ( $this->data ['select_list'] );
	}
	public function saveOrder() {
	}
	public function addnew() {
		$this->setTemplate ( 'accountsForm' );
		
		$form = new SysAccountsForm ();
		if ($this->data ['is_post']) {
			$form->bind ( $this->formData );
			/*foreach($form->getErrorSchema()->getErrors() as $a){
				echo $a->__toString();;
			}
			die;*/
			if ($form->isValid ()) {
				$product_id = SysAccounts::insertUpdate ( $this->formData );
				if ($product_id > 0) {
					$this->data ['result'] = __ ( 'add_new_success' );
					$form = new SysAccountsForm ();
				} else {
					$this->data ['result'] = sprintf ( __ ( 'add_new_error' ), sfConfig::get ( 'app_email_admin' ) );
				}
			}
		}
		$this->setVar ( 'form', $form );
	}
	public function edit() {
		$this->setTemplate ( 'accountsForm' );
		
		$form = new SysAccountsForm ();
		if ($this->data ['is_post']) {
			$form->bind ( $this->formData );
			if ($form->isValid ()) {
				$this->formData ['id'] = $this->data ['id'];
				if (SysAccounts::insertUpdate ( $this->formData )) {
					$this->data ['result'] = __ ( 'edit_success' );
				} else {
					$this->data ['result'] = sprintf ( __ ( 'edit_error' ), sfConfig::get ( 'app_email_admin' ) );
				}
			}
		} else {
			$data = SysAccounts::getDataByIdAdmin ( $this->data ['id'] );
			if (count ( $data ) < 0) {
				$this->redirect ( $this->data ['redirect'] );
			} else {
				$this->formData = array (
						'group_id'			=> $data['group_id'],
						'name' 					=> $data ['name'],
						'login_user'		=> $data['login_user'],
						'email' 				=> $data ['email'],
						'is_enabled'		=> $data ['is_enabled'],
						'_csrf_token' 	=> $form->getCSRFToken () 
				);
				
				$form->bind ( $this->formData );
			}
		}
		$this->setVar ( 'form', $form );
		$this->data['is_update'] = 1;
	}
}